package com.example.backend.controller;

import com.example.backend.common.ResultMessage;
import com.example.backend.entity.ShenFen;
import com.example.backend.service.impl.ShenFenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/payment")
public class PaymentController {
    @Autowired
    private ShenFenService shenFenService;
    /**
     * 获取用户余额
     * @param yhid 用户 ID
     * @return 用户信息
     */
    @GetMapping("/balance/{yhid}")
    public ResultMessage getBalance(@PathVariable Integer yhid) {
        try {
            ShenFen user = shenFenService.queryShenFenById(yhid);
            if (user != null && user.getBalance() != null) {
                return ResultMessage.success(user.getBalance(), "获取余额成功");
            }
            return ResultMessage.error("用户不存在或余额信息为空");
        } catch (Exception e) {
            return ResultMessage.error("获取余额失败: " + e.getMessage());
        }
    }

    /**
     * 用户充值
     * @param yhid 用户 ID
     * @param amount 充值金额
     * @return 充值结果
     */
    @PutMapping("/recharge/{yhid}")
    public ResultMessage recharge(@PathVariable Integer yhid,@RequestBody Double amount) {
        boolean success = shenFenService.recharge(yhid, amount);
        return success ? ResultMessage.success(null, "充值成功") : ResultMessage.error("充值失败");
    }

}
